﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" doctype-public="-//W3C//DTD XHTML 1.1//EN"/>

<xsl:variable name="backgrcolor" select="'#2e2e2e'" />

<xsl:variable name="c0" select="'#ffffff'" /> <!-- White -->
<xsl:variable name="c1" select="'#ff0000'" /> <!-- Red -->
<xsl:variable name="c2" select="'#1eb626'" /> <!-- Set Green -->
<xsl:variable name="c3" select="'#4850b8'" /> <!-- Magic Blue -->
<xsl:variable name="c4" select="'#968269'" /> <!-- Unique Gold -->
<xsl:variable name="c5" select="'#9C9C9C'" /> <!-- Superior/Eth Grey -->
<xsl:variable name="c6" select="'#000000'" /> <!-- Black -->
<xsl:variable name="c7" select="'#968269'" /> <!-- Unique Gold -->
<xsl:variable name="c8" select="'#ff8000'" /> <!-- Crafted Amber -->
<xsl:variable name="c9" select="'#FFE000'" /> <!-- Rare DarkYellow -->
<xsl:variable name="cdef" select="'#ffffff'" /> <!-- Default -->

<xsl:variable name="keptcolor" select="'#1eb626'" />
<xsl:variable name="soldcolor" select="'#ff0000'" />
<xsl:variable name="seencolor" select="'#9C9C9C'" />

<xsl:template name="break-replace">
	<xsl:param name="value" />
	
	<xsl:choose>
		<xsl:when test="contains($value,'|')">
			<xsl:choose>
				<xsl:when test="string-length(substring-before($value,'|')) &lt; '4'">
					<div style= "text-align: center;">
						<br/>
					</div>
				</xsl:when>
				<xsl:otherwise>
					<div style= "text-align: center;">
						<xsl:call-template name="ccode-replace">
							<xsl:with-param name="value" select="substring-before($value,'|')" />
						</xsl:call-template>
					</div>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:call-template name="break-replace">
				<xsl:with-param name="value" select="substring-after($value,'|')" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<div style= "text-align: center;">
				<xsl:call-template name="ccode-replace">
					<xsl:with-param name="value" select="$value" />
				</xsl:call-template>
			</div>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="ccode-replace">
	<xsl:param name="value" />

	<xsl:variable name="color">
		<xsl:choose>
			<!-- White -->
			<xsl:when test="starts-with($value,'ÿc0')">
				<xsl:copy-of select="$c0" />
			</xsl:when>
			<!-- Red -->
			<xsl:when test="starts-with($value,'ÿc1')">
				<xsl:copy-of select="$c1" />
			</xsl:when>
			<!-- Set Green -->
			<xsl:when test="starts-with($value,'ÿc2')">
				<xsl:copy-of select="$c2" />
			</xsl:when>
			<!-- Magic Blue -->
			<xsl:when test="starts-with($value,'ÿc3')">
				<xsl:copy-of select="$c3" />
			</xsl:when>
			<!-- Unique Gold -->
			<xsl:when test="starts-with($value,'ÿc4')">
				<xsl:copy-of select="$c4" />
			</xsl:when>
			<!-- Superior/Eth Grey -->
			<xsl:when test="starts-with($value,'ÿc5')">
				<xsl:copy-of select="$c5" />
			</xsl:when>
			<!-- Black -->
			<xsl:when test="starts-with($value,'ÿc6')">
				<xsl:copy-of select="$c6" />
			</xsl:when>
			<!-- Unique Gold -->
			<xsl:when test="starts-with($value,'ÿc7')">
				<xsl:copy-of select="$c7" />
			</xsl:when>
			<!-- Crafted Amber -->
			<xsl:when test="starts-with($value,'ÿc8')">
				<xsl:copy-of select="$c8" />
			</xsl:when>
			<!-- Rare DarkYellow -->
			<xsl:when test="starts-with($value,'ÿc9')">
				<xsl:copy-of select="$c9" />
			</xsl:when>
			<xsl:otherwise>
				#ffffff
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:choose>
		<xsl:when test="contains($value,'#')">
			<font style= "color: {$color};">
				<xsl:value-of select="substring-after(substring-before($value,'#'),substring($value,1,3))"/>
			</font>
			<xsl:call-template name="ccode-replace">
               <xsl:with-param name="value" select="substring-after($value,'#')" />
            </xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<font style= "color: {$color};">
				<xsl:value-of select="substring-after($value,substring($value,1,3))"/>
			</font>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="apply-color">
	<xsl:param name="quality" />
	<xsl:param name="iseth" />
	
	<xsl:variable name="qualitycolor">
		<xsl:choose>
			<!-- Low Quality White -->
			<xsl:when test="$quality = '1'">
				<xsl:copy-of select="$c0" />
			</xsl:when>
			<!-- Normal White-->
			<xsl:when test="$quality = '2' and $iseth = '0'">
				<xsl:copy-of select="$c0" />
			</xsl:when>
			<!-- Normal Ethereal Grey -->
			<xsl:when test="$quality = '2' and $iseth = '1'">
				<xsl:copy-of select="$c5" />
			</xsl:when>			
			<!-- Superior Grey -->
			<xsl:when test="$quality = '3'">
				<xsl:copy-of select="$c5" />
			</xsl:when>
			<!-- Magic Blue -->
			<xsl:when test="$quality = '4'">
				<xsl:copy-of select="$c3" />
			</xsl:when>
			<!-- Set Green -->
			<xsl:when test="$quality = '5'">
				<xsl:copy-of select="$c2" />
			</xsl:when>
			<!-- Rare DarkYellow -->
			<xsl:when test="$quality = '6'">
				<xsl:copy-of select="$c9" />
			</xsl:when>
			<!-- Unique Gold -->
			<xsl:when test="$quality = '7'">
				<xsl:copy-of select="$c4" />
			</xsl:when>
			<!-- Crafted Amber -->
			<xsl:when test="$quality = '8'">
				<xsl:copy-of select="$c8" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy-of select="'#ff00ff'" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
		
	<div style="display: block; color: {$qualitycolor}; font-weight: bold; float: left; width: 200px; margin-right: 10px; margin-left: 10px;text-align: center;">
		<xsl:value-of select="@typedesc"/>
	</div>
	
	<span style="color: {$qualitycolor}; font-weight: bold" onMouseOut="hideTT()">
		<xsl:attribute name="onMouseOver">
			showTT('<xsl:value-of select="@id"/>')
		</xsl:attribute>
		<xsl:value-of select="@name"/>
	</span>
	
	<br/>
	<div class="tooltip">
		<xsl:attribute name="id">
			<xsl:value-of select="@id"/>
		</xsl:attribute>
		
		<div style="color: {$qualitycolor}; font-weight: bold;text-align: center;">
			<xsl:value-of select="@name"/>
		</div>
		
		<span>
			<xsl:call-template name="break-replace">
				<xsl:with-param name="value" select="." />
			</xsl:call-template>
		</span>
	</div>

</xsl:template>

<xsl:template match="itemlog">

	<html>
		<head>			
			<title>Itemlog</title>
			<style type="text/css">
			.tooltip {
				opacity: 0.7;
				position: absolute;
				display: none;
				background: #000000;
				padding: 10px;
			}
			#searchbox {
			   position: fixed;
			   left: 3px;
			   bottom: 3px;
			   padding-left: 20px;
			   padding-right: 20px;
			   padding-top: 5px;
			   padding-bottom: 10px;
			   background-color: #1F1F1F;
			   color: #ffffff;
			   display: none;			   
			}
			</style>
			<script>
				var keptitems = new Array();
				var solditems = new Array();
				var seenitems = new Array();
				var i = 0;
				var n = 0;
				var m = 0;
				<xsl:for-each select="/itemlog/item">
					<xsl:if test="@mode = '0'">
						keptitems[i] = new Array(2);
						keptitems[i]['id'] = <xsl:value-of select="@id"/>;
						keptitems[i]['name'] = "<xsl:value-of select="@name"/>";
						keptitems[i]['ilvl'] = "<xsl:value-of select="@ilvl"/>";
						keptitems[i]['type'] = "<xsl:value-of select="@type"/>";
						keptitems[i]['typedesc'] = "<xsl:value-of select="@typedesc"/>";
						keptitems[i]['location'] = "<xsl:value-of select="@typedesc"/>";
						i++;
					</xsl:if>
					<xsl:if test="@mode = '1'">
						solditems[n] = new Array(2);
						solditems[n]['id'] = <xsl:value-of select="@id"/>;
						solditems[n]['name'] = "<xsl:value-of select="@name"/>";
						solditems[n]['ilvl'] = "<xsl:value-of select="@ilvl"/>";
						solditems[n]['type'] = "<xsl:value-of select="@type"/>";
						solditems[n]['typedesc'] = "<xsl:value-of select="@typedesc"/>";
						solditems[n]['location'] = "<xsl:value-of select="@typedesc"/>";
						n++;
					</xsl:if>
					<xsl:if test="@mode = '2'">
						seenitems[m] = new Array(2);
						seenitems[m]['id'] = <xsl:value-of select="@id"/>;
						seenitems[m]['name'] = "<xsl:value-of select="@name"/>";
						seenitems[m]['ilvl'] = "<xsl:value-of select="@ilvl"/>";
						seenitems[m]['type'] = "<xsl:value-of select="@type"/>";
						seenitems[m]['typedesc'] = "<xsl:value-of select="@typedesc"/>";
						seenitems[m]['location'] = "<xsl:value-of select="@typedesc"/>";
						m++;
					</xsl:if>
				</xsl:for-each>
				
				var line = "";

				function showAll() {
					var noresult = document.getElementById("noresult");
					noresult.style.display = "none";
					for(i=0;i &lt; keptitems.length; i++) {
						line = document.getElementById("l" + keptitems[i]['id']);
						line.style.display = "block";
					}
					for(i=0;i &lt; solditems.length; i++) {
						line = document.getElementById("l" + solditems[i]['id']);
						line.style.display = "block";
					}
					for(i=0;i &lt; seenitems.length; i++) {
						line = document.getElementById("l" + seenitems[i]['id']);
						line.style.display = "none";
					}
				}
				
				function showKept() {
					var noresult = document.getElementById("noresult");
					noresult.style.display = "none";
					for(i=0;i &lt; keptitems.length; i++) {
						line = document.getElementById("l" + keptitems[i]['id']);
						line.style.display = "block";
					}
					for(i=0;i &lt; solditems.length; i++) {
						line = document.getElementById("l" + solditems[i]['id']);
						line.style.display = "none";
					}
					for(i=0;i &lt; seenitems.length; i++) {
						line = document.getElementById("l" + seenitems[i]['id']);
						line.style.display = "none";
					}
				}

				function showSold() {
					var noresult = document.getElementById("noresult");
					noresult.style.display = "none";
					for(i=0;i &lt; keptitems.length; i++) {
						line = document.getElementById("l" + keptitems[i]['id']);
						line.style.display = "none";
					}
					for(i=0;i &lt; solditems.length; i++) {
						line = document.getElementById("l" + solditems[i]['id']);
						line.style.display = "block";
					}
					for(i=0;i &lt; seenitems.length; i++) {
						line = document.getElementById("l" + seenitems[i]['id']);
						line.style.display = "none";
					}
				}
				
				function showSeen() {
					var noresult = document.getElementById("noresult");
					noresult.style.display = "none";
					for(i=0;i &lt; keptitems.length; i++) {
						line = document.getElementById("l" + keptitems[i]['id']);
						line.style.display = "none";
					}
					for(i=0;i &lt; solditems.length; i++) {
						line = document.getElementById("l" + solditems[i]['id']);
						line.style.display = "none";
					}					
					for(i=0;i &lt; seenitems.length; i++) {
						line = document.getElementById("l" + seenitems[i]['id']);
						line.style.display = "block";
					}
				}			

				function showNone() {
					for(i=0;i &lt; keptitems.length; i++) {
						line = document.getElementById("l" + keptitems[i]['id']);
						line.style.display = "none";
					}
					for(i=0;i &lt; solditems.length; i++) {
						line = document.getElementById("l" + solditems[i]['id']);
						line.style.display = "none";
					}
					for(i=0;i &lt; seenitems.length; i++) {
						line = document.getElementById("l" + seenitems[i]['id']);
						line.style.display = "none";
					}
				}
				
				function searchName(e) {
					var ENTER_KEY = 13;
					var code = "";

					if (window.event) { // IE
						code = e.keyCode;
					} else if (e.which) { // Netscape/Firefox/Opera
						code = e.which;
					}

					if(code == ENTER_KEY) {
						var noresult = document.getElementById("noresult");
						noresult.style.display = "none";

						var input = document.getElementById("search");
						var string = input.value;
						var keptbox = document.getElementById("keptbox");
						var soldbox = document.getElementById("soldbox");
						var seenbox = document.getElementById("seenbox");
						var searchresults = 0;
						var search = eval("/" + string + "/i");
						showNone();
	
						if(keptbox.checked == true) {
							for(i=0;i &lt; keptitems.length; i++) {
								var itemname = keptitems[i]['name'];
								var result = search.test(itemname);
								if(result == true) {
									searchresults++;
									
									line = document.getElementById("l" + keptitems[i]['id']);
									line.style.display = "block";
								}
							}
						}

						if(soldbox.checked == true) {
							for(i=0;i &lt; solditems.length; i++) {
								var itemname = solditems[i]['name'];
								var result = search.test(itemname);
								if(result == true) {
									searchresults++;
						
									line = document.getElementById("l" + solditems[i]['id']);
									line.style.display = "block";
								}
							}
						}
						
						if(seenbox.checked == true) {
							for(i=0;i &lt; seenitems.length; i++) {
								var itemname = seenitems[i]['name'];
								var result = search.test(itemname);
								if(result == true) {
									searchresults++;
						
									line = document.getElementById("l" + seenitems[i]['id']);
									line.style.display = "block";
								}
							}
						}

						if(searchresults == 0) {
							var noresult = document.getElementById("noresult");
							noresult.style.display = "block";
						}
					}
				}
								
				//////////////////////////////
				// Popup Functions
				//////////////////////////////
				tt = null;
 
				document.onmousemove = updateTT;
				 
				function updateTT(e) {
				  if (tt != null) {
					x = (document.all) ? window.event.x + tt.offsetParent.scrollLeft : e.pageX;
					y = (document.all) ? window.event.y + tt.offsetParent.scrollTop  : e.pageY;
					tt.style.left = (x+15) + "px";
					tt.style.top   = (y)-20 + "px";
				  }
				}
				 
				function showTT(id) {
				  tt = document.getElementById(id);
				  tt.style.display = "block"
				}
				 
				function hideTT() {
				  tt.style.display = "none";
				}
				//////////////////////////////
				// Search key events
				//////////////////////////////
            function handleSearch(evt) {
               var charCode = (window.event) ? event.keyCode : evt.which;
               var char = String.fromCharCode(charCode);
               var sb = document.getElementById("searchbox");
               var st = document.getElementById("search");
               
               if ( /[A-Z]/.test(char))
               {
                  sb.style.display = "block";
                  st.focus();
               }
               else if (charCode == 27)
               {
                  var sb = document.getElementById("searchbox");
                  sb.style.display = "none";
                  st.value = "";
                  showAll();
               }
               
               return true;
            }
            
            window.document.onkeydown= handleSearch;
			</script>
		</head>
		<body onkeypress="searchPress();" style="font: 14px AvQest; background-color:{$backgrcolor};">

			<span style="background-color:{$backgrcolor};">
				<span style="padding-left: 10px;">
					<input type="button" value="Show Kept/Sold" onClick="showAll();" style="color: #000000; font-family: AvQest;"/>
				</span>
				<span style="padding-left: 50px;">
					<input type="button" value="Show Kept" onClick="showKept()" style="color: {$keptcolor}; font-family: AvQest;"/>
					<input type="button" value="Show Sold" onClick="showSold()" style="color: {$soldcolor}; font-family: AvQest;"/>
					<input type="button" value="Show Ignored" onClick="showSeen()" style="color: {$seencolor}; font-family: AvQest;"/>
				</span>
				<span id="searchbox">
					Search Item Name: <input type="text" id="search" onkeypress="searchName(event)" style="color: #000000; font-family: AvQest;"/>
					<span style="padding-left: 10px;color: #ffffff;">
						Kept <input type="checkbox" id="keptbox" checked="checked"/>
						Sold <input type="checkbox" id="soldbox" checked="checked"/>
						Seen <input type="checkbox" id="seenbox" checked="checked"/>
					</span>
				</span>
				<!--
				<br/>
				<br/>
				
				<span style="padding-left: 50px; color: #ffffff; text-align:center;">
					<span>
						Filters: 
					</span>
					<span>
						Normal <input type="checkbox" id="normalbox" checked="checked" style=""/>
					</span>
					<span>
						Superior <input type="checkbox" id="superiorbox" checked="checked"/>
					</span>
					<span>
						Magic <input type="checkbox" id="magicbox" checked="checked"/>
					</span>				
					<span>
						Set <input type="checkbox" id="setbox" checked="checked"/>
					</span>
					<span>
						Rare <input type="checkbox" id="rarebox" checked="checked"/>
					</span>
					<span>
						Unique <input type="checkbox" id="uniquebox" checked="checked"/>
					</span>
					<span>
						Crafted <input type="checkbox" id="craftedbox" checked="checked"/>
					</span>
				</span>
				-->
				<br/>
				<br/>
				<br/>
				
				<main>
					<span style="display: block; float: left; width: 175px; margin-right: 10px;margin-left: 5px; color: #ffffff; font-weight: bold; text-align: center;">
							Time
					</span>
					<span style="display: block; float: left; width: 175px; margin-right: 10px;margin-left: 5px; color: #ffffff; font-weight: bold; text-align: center;">
							Found By
					</span>
					<div style="float: left; width: 225px; margin-right: 10px;color: #ffffff; font-weight: bold;">
							Found At
					</div>
					<div style="display: block; float: left; width: 50px; margin-right: 10px;color: #ffffff; font-weight: bold; text-align: center;">
							Action
					</div>
					<div style="display: block; float: left; width: 30px; margin-right: 0px; margin-left: 20px;color: #ffffff; font-weight: bold; text-align: center;">
							Ilvl
					</div>
					<div style="display: block; float: left; width: 200px; margin-right: 0px;margin-left: 20px;color: #ffffff; font-weight: bold; text-align: center;">
							Type
					</div>
					<div style="display: block; float: left; margin-right: 10px; margin-left: 10px;color: #ffffff; font-weight: bold;text-align: left;">
							Name
					</div>
				</main>
				
				<br/>
				<br/>
			</span>
			
			<div id="results" style="margin-left: 10px;">
			<xsl:for-each select="/itemlog/item">

				<xsl:sort select="@id" order="descending" data-type="number" />
				<div id="noresult" style="display: none;font-weight: bold;color: #FF0000;text-align: center;">Your search returned no result!</div>
				<div>
					<xsl:attribute name="id">l<xsl:value-of select="@id"/></xsl:attribute>
					
					<xsl:if test="@mode = '2'">
						<xsl:attribute name="style">display: none;</xsl:attribute>
					</xsl:if>
				
					<div style="display: block;color: #ffffff; float: left; width: 175px; margin-right: 5px; text-align: left;">
						<xsl:value-of select="@time"/>
					</div>
					
					<div style="display: block;color: #ffffff; float: left; width: 175px; margin-right: 10px; margin-left: 10px; text-align: center;">
						<xsl:value-of select="@char"/>
					</div>
					
					<div style="display: block;color: #ffffff; float: left; width: 205px; margin-right: 10px; margin-left: 10px;">
						<xsl:value-of select="@location"/>
					</div>
					
					<div style="display: block;color: #ffffff; float: left; width: 55px; margin-right: 10px; margin-left: 10px;">
						<xsl:choose>
							<xsl:when test="@mode = '0'">
								<font style="color: {$keptcolor};">
									Kept
								</font>
							</xsl:when>
							<xsl:when test="@mode = '1'">
								<font style="color: {$soldcolor};">
									Sold
								</font>
							</xsl:when>
							<xsl:when test="@mode = '2'">
								<font style="color: {$seencolor};">
									Ignored
								</font>
							</xsl:when>
						</xsl:choose>
					</div>
					<div style="display: block;color: #ffffff; float: left; width: 30px; margin-right: 10px; margin-left: 10px;text-align: center;">
						<xsl:value-of select="@ilvl"/>
					</div> 

					<xsl:call-template name="apply-color">
					   <xsl:with-param name="quality" select="@quality" />
					   <xsl:with-param name="iseth" select="@ethereal" />
					</xsl:call-template>
					
				</div>
			</xsl:for-each>
			</div>
		</body>
	</html>
</xsl:template>

</xsl:stylesheet>